package com.usian.wemedia.controller.v1;

import com.usian.apis.media.WmNewsControllerApis;
import com.usian.model.admin.dtos.NewsAuthDto;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.dtos.WmNewsDto;
import com.usian.model.media.dtos.WmNewsPageReqDto;
import com.usian.model.media.pojos.WmNews;
import com.usian.wemedia.service.WmNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/news")
public class WmNewsController implements WmNewsControllerApis {


    @Autowired
    private WmNewsService wmNewsService;

    @Override
    @PostMapping("list")
    public ResponseResult findList(@RequestBody WmNewsPageReqDto dto) {
        return wmNewsService.findList(dto);
    }

    @Override
    @PostMapping("submit")
    public ResponseResult submitNews(@RequestBody WmNewsDto dto) {

        if (dto.getStatus().shortValue()== WmNews.Status.NORMAL.getCode()){
            return wmNewsService.submitNews(dto,WmNews.Status.NORMAL.getCode());
        }else if (dto.getStatus().shortValue()== WmNews.Status.SUBMIT.getCode()){
            return wmNewsService.submitNews(dto,WmNews.Status.NORMAL.getCode());
        }

        return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
    }

    @Override
    @GetMapping("one/{id}")
    public ResponseResult queryNewsById(@PathVariable Integer id) {
        return wmNewsService.queryNewsById(id);
    }

    @Override
    @GetMapping("del_news/{id}")
    public ResponseResult delNews(@PathVariable Integer id) {
        return wmNewsService.delNews(id);
    }

    @Override
    @PostMapping("down_or_up")
    public ResponseResult downOrUpNews(@RequestBody WmNewsDto dto) {
        return wmNewsService.downOrUpNews(dto);
    }

    @Override
    @GetMapping("findNewsById/{id}")
    public WmNews findNewsById(@PathVariable Integer id){
        return wmNewsService.getById(id);
    }


    @Override
    @PostMapping("updateNewStatus")
    public ResponseResult updateWmNews(@RequestBody WmNews wmNews){
        wmNewsService.updateById(wmNews);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    @GetMapping("findRelease")
    public List<WmNews> findRelease() {

        return wmNewsService.findRelease();
    }


    @PostMapping("findList")
    @Override
    public ResponseResult findList(@RequestBody NewsAuthDto authDto){

        return wmNewsService.findListAndPage(authDto);
    }

    @Override
    @GetMapping("findOne/{id}")
    public ResponseResult findOne(@PathVariable Integer id){
        return wmNewsService.findOne(id);
    }


}
